package constant.milk.cashbook.budgetstats;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.bean.BudgetData;
import constant.milk.cashbook.bean.StatsData;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.tip.TipInfoActivity;
import constant.milk.cashbook.tip.TipMap;
import constant.milk.cashbook.util.MilkUtil;

public class BudgetActivity extends SuperActivity
{
	private static final int REQUEST_AUTO_IN_UPDATE = 0;
	
	private View weekdayView;
	private View monthView;
	private View yearView;
	
	private View preArrowView;
	private View nextArrowView;
	private TextView dateTextView;
	
	private TextView budgetMoneyTextView;
	private TextView useMoneyTextView;
	private TextView budgetBalanceTextView;
	
	private Button updateButton;
	
	private int year;
	private int month;
	private int day;
	
	private int year1;
	private int month1;
	private int day1;
	
	private int year2;
	private int month2;
	private int day2;
	
	private DBSelectQuery select;
	
	private int type;
	private int kind;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.budget_activity);
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		weekdayView = findViewById(R.id.budgetWeekdayView);
		monthView = findViewById(R.id.budgetMonthView);
		yearView = findViewById(R.id.budgetYearView);
		
		preArrowView = findViewById(R.id.budgetPreView);
		nextArrowView = findViewById(R.id.budgetNextView);
		dateTextView = (TextView) findViewById(R.id.budgetDateTextView);
		
		budgetMoneyTextView = (TextView) findViewById(R.id.budgetMoneyTextView);
		useMoneyTextView = (TextView) findViewById(R.id.budgetUseMoneyTextView);
		budgetBalanceTextView = (TextView) findViewById(R.id.budgetBalanceTextView);
		
		updateButton = (Button) findViewById(R.id.updateButton);
	}

	@Override
	protected void setOnClick()
	{
		weekdayView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				kind = 0;
				
				setSettingData();
			}
		});
		
		monthView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				kind = 1;
				
				setSettingData();
			}
		});
		
		yearView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				kind = 2;
				
				setSettingData();
			}
		});
		
		preArrowView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				preDate();
			}
		});
		
		nextArrowView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				nextDate();
			}
		});
		
		updateButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, BudgetUpdateActivity.class);
				intent.putExtra(BudgetStatsActivity.TYPE, type);
				startActivityForResult(intent, REQUEST_AUTO_IN_UPDATE);
			}
		});
	}

	@Override
	protected void setData()
	{
		select = new DBSelectQuery(superContext, superDb);
		
		Intent intent = getIntent();
		type = intent.getIntExtra(BudgetStatsActivity.TYPE, 0);
		
		Calendar cal = Calendar.getInstance();
		
		year = cal.get(Calendar.YEAR);
		month = cal.get(Calendar.MONTH);
		day = cal.get(Calendar.DAY_OF_MONTH);
		
		if (type == BudgetStatsActivity.OUT)
		{
			setTitle("지출예산");
		}
		else if (type == BudgetStatsActivity.IN)
		{
			setTitle("수입예산");
		}
		else if (type == BudgetStatsActivity.MOVE)
		{
			setTitle("이동예산");
		}
		
		kind = 0;
		
		setSettingData();
	}
	
	private void preDate()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		
		if (kind == 0)
		{
			cal.add(Calendar.DAY_OF_MONTH, -7);
		}
		else if (kind == 1)
		{
			cal.add(Calendar.MONTH, -1);
		}
		else if (kind == 2)
		{
			cal.add(Calendar.YEAR, -1);
		}
		
		year = cal.get(Calendar.YEAR);
		month = cal.get(Calendar.MONTH);
		day = cal.get(Calendar.DAY_OF_MONTH);
		
		setSettingData();
	}
	
	private void nextDate()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		
		if (kind == 0)
		{
			cal.add(Calendar.DAY_OF_MONTH, 7);
		}
		else if (kind == 1)
		{
			cal.add(Calendar.MONTH, 1);
		}
		else if (kind == 2)
		{
			cal.add(Calendar.YEAR, 1);
		}
		
		year = cal.get(Calendar.YEAR);
		month = cal.get(Calendar.MONTH);
		day = cal.get(Calendar.DAY_OF_MONTH);
		
		setSettingData();
	}
	
	private void setDateWeekday()
	{
		Calendar cal1 = Calendar.getInstance();
		cal1.set(year, month, day);
		
		Calendar cal2 = Calendar.getInstance();
		cal2.set(year, month, day);
		
		Calendar cal3 = Calendar.getInstance();
		cal3.set(year, month, day);
		
		int weekday = cal2.get(Calendar.DAY_OF_WEEK);
		
		cal1.add(Calendar.DAY_OF_MONTH, -(weekday - 1));
		cal2.add(Calendar.DAY_OF_MONTH, (7 - weekday));
		
		year1 = cal1.get(Calendar.YEAR);
		month1 = cal1.get(Calendar.MONTH);
		day1 = cal1.get(Calendar.DAY_OF_MONTH);
		
		year2 = cal2.get(Calendar.YEAR);
		month2 = cal2.get(Calendar.MONTH);
		day2 = cal2.get(Calendar.DAY_OF_MONTH);
		
		dateTextView.setText(year + "년 " + (month+1) + "월 " + cal3.get(Calendar.WEEK_OF_MONTH) + "째주");
	}
	
	private void setDateMonth()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		
		year1 = cal.get(Calendar.YEAR);
		month1 = cal.get(Calendar.MONTH);
		day1 = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		
		year2 = cal.get(Calendar.YEAR);
		month2 = cal.get(Calendar.MONTH);
		day2 = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		dateTextView.setText(year + "년 " + (month+1) + "월 ");
	}
	
	private void setDateYear()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		
		year1 = cal.get(Calendar.YEAR);
		month1 = cal.getActualMinimum(Calendar.MONTH);
		day1 = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		
		year2 = cal.get(Calendar.YEAR);
		month2 = cal.getActualMaximum(Calendar.MONTH);
		day2 = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		dateTextView.setText(year + "년");
	}
	
	private void setBudgetData(int kind)
	{
		BudgetData data = select.getBudget(type, kind);
		
		budgetMoneyTextView.setText(MilkUtil.getWon(data.getMoney()));
		
		ArrayList<StatsData> list = new ArrayList<StatsData>();
		
		ArrayList<StatsData> changeList = new ArrayList<StatsData>();
		
		for (int i = 0; i < 6; i++)
		{
			ArrayList<StatsData> statsList = select.getInOutStatsList(type, i, 
					MilkUtil.getDateDB(year1, month1+1, day1), 
					MilkUtil.getDateDB(year2, month2+1, day2));
			
			list.addAll(statsList);
			
			if (type == 0)
			{
				ArrayList<StatsData> changeStatsList = select.getInOutStatsList(-1, i, 
						MilkUtil.getDateDB(year1, month1+1, day1), MilkUtil.getDateDB(year2, month2+1, day2));
				
				changeList.addAll(changeStatsList);
			}
		}
		
		BigDecimal total = BigDecimal.ZERO;
		
		for (int i = 0; i < list.size(); i++)
		{
			total = total.add(new BigDecimal(list.get(i).getMoney()));
			
			if (type == 0 && changeList != null)
			{
				for (int j = 0; j < changeList.size(); j++)
				{
					if (list.get(i).getTitle().equals(changeList.get(j).getTitle()))
					{
						total = total.subtract(new BigDecimal(changeList.get(j).getMoney()));
					}
				}
			}
		}
		
		useMoneyTextView.setText(MilkUtil.getWon(total.toString()));
		
		BigDecimal balance = BigDecimal.ZERO;
		
		balance = new BigDecimal(data.getMoney()).subtract(total);
		
		budgetBalanceTextView.setText(MilkUtil.getWon(balance.toString()));
	}
	
	private void setWeekday()
	{
		int padding = MilkUtil.dipToPixel(superContext, 10);
		weekdayView.setBackgroundResource(R.drawable.btn_neon_push);
		weekdayView.setPadding(padding, padding, padding, padding);
		monthView.setBackgroundResource(0);
		monthView.setPadding(padding, padding, padding, padding);
		yearView.setBackgroundResource(0);
		yearView.setPadding(padding, padding, padding, padding);
		
		setDateWeekday();
		setBudgetData(0);
	}
	
	private void setMonth()
	{
		int padding = MilkUtil.dipToPixel(superContext, 10);
		weekdayView.setBackgroundResource(0);
		weekdayView.setPadding(padding, padding, padding, padding);
		monthView.setBackgroundResource(R.drawable.btn_neon_push);
		monthView.setPadding(padding, padding, padding, padding);
		yearView.setBackgroundResource(0);
		yearView.setPadding(padding, padding, padding, padding);
		
		setDateMonth();
		setBudgetData(1);
	}
	
	private void setYear()
	{
		int padding = MilkUtil.dipToPixel(superContext, 10);
		weekdayView.setBackgroundResource(0);
		weekdayView.setPadding(padding, padding, padding, padding);
		monthView.setBackgroundResource(0);
		monthView.setPadding(padding, padding, padding, padding);
		yearView.setBackgroundResource(R.drawable.btn_neon_push);
		yearView.setPadding(padding, padding, padding, padding);
		
		setDateYear();
		setBudgetData(2);
	}
	
	private void setSettingData()
	{
		if (kind == 0)
		{
			setWeekday();
		}
		else if (kind == 1)
		{
			setMonth();
		}
		else if (kind == 2)
		{
			setYear();
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == REQUEST_AUTO_IN_UPDATE)
		{
			setSettingData();
		}
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(superContext, TipInfoActivity.class);
		
		if (type == BudgetStatsActivity.OUT)
		{
			intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_BUDGER_OUT);
		}
		else if (type == BudgetStatsActivity.IN)
		{
			intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_BUDGER_IN);
		}
		else if (type == BudgetStatsActivity.MOVE)
		{
			intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_BUDGER_MOVE);
		}
		startActivity(intent);
	}

}
